package com.sxj.PDMS.controller.admin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.sxj.PDMS.common.BaseResponse;
import com.sxj.PDMS.common.ErrorCode;
import com.sxj.PDMS.common.Result;
import com.sxj.PDMS.exception.BusinessException;
import com.sxj.PDMS.model.entity.StudentUploadData;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.service.StudentUploadDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminPersonalFileController {
    @Autowired
    private StudentUploadDataService studentUploadDataService;

    @GetMapping("/selectAll_personalFile")
    public BaseResponse<PagingQueryResponse<StudentUploadData>> selectPersonalFile(@RequestParam(defaultValue = "1") Integer page,
                                                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询 ：page:{}, pageSize:{}", page, pageSize);
        PagingQueryResponse<StudentUploadData> pagingQueryResponse = studentUploadDataService.selectAll(page, pageSize);
        return Result.success(pagingQueryResponse);

    }

    @GetMapping("/select_personalFile")
    public BaseResponse<PagingQueryResponse<StudentUploadData>> selectWithCondition(@RequestParam(defaultValue = "1") Integer page,
                                                                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                                                                    Date time,
                                                                                    String studentNumber) {
        log.info("date:{}, studentNumber:{}", time, studentNumber);

        PagingQueryResponse<StudentUploadData> pagingQueryResponse = studentUploadDataService.studentUploadDataSelect(page, pageSize, time, studentNumber);

        return Result.success(pagingQueryResponse);
    }

    @DeleteMapping("/delete_personalFile/{serialNumber}")
    public BaseResponse<Boolean> deletePersonalFile(@PathVariable Long serialNumber) {
        log.info("serialNumber:{}", serialNumber);

        //todo：/学生提交数据的存放路径
        String path = "D:\\user\\Desktop\\store\\student_files\\";
        if (serialNumber == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StudentUploadData studentUploadData1 = studentUploadDataService.getById(serialNumber);
        if (studentUploadData1 == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "编号所指数据不存在");
        }

        String flag = studentUploadData1.getDataAddress();
        boolean removed = studentUploadDataService.removeById(serialNumber);
        if (!removed) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        List<String> fileNames = FileUtil.listFileNames(path);
        String avatar = fileNames.stream().filter(name -> name.contains(flag)).findAny().orElse("");
        if (StrUtil.isNotEmpty(avatar)) {
            boolean isDeleted = FileUtil.del(path + avatar);
            if (isDeleted) {
                log.info("文件 {} 删除成功。", avatar);
            } else {
                log.error("文件 {} 删除失败。", avatar);
            }
        } else {
            log.warn("文件 {} 不存在，无需删除。", avatar);
        }
        return Result.success(true);
    }
}
